package T4.EjemplosDeIO;


import java.io.*;
import java.net.*;

public class Ejemplo7 {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
        try {
            serverSocket = new ServerSocket(8081);
            socket = serverSocket.accept();
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();
            dataInputStream = new DataInputStream(is);
            System.out.println(dataInputStream.readLine());
            System.out.println(dataInputStream.readInt());
            dataOutputStream = new DataOutputStream(os);
            dataOutputStream.writeBytes("Hola que tal; yo soy el servidor\n");
            dataOutputStream.writeFloat(2.3F);

        } catch (Exception ex) {
            System.out.println("Fallo en la red");
            ex.printStackTrace();
        } finally {
            try {
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (dataInputStream != null) {
                    dataInputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}